/**
 * 功能:TK_BBS_TOPIC 控制器类
 * 开发人员:chengd
 * 创建时间:2012-05-17 16:02:13
 */
package com.towker.frame.controller.complex;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.towker.web.MyRequest;
import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.frame.model.complex.BbsReply;
import com.towker.frame.model.complex.BbsTopic;
import com.towker.frame.service.complex.BbsReplyService;
import com.towker.frame.service.complex.BbsTopicService;
import com.towker.frame.share.sys.user.UserShare;

//TK_BBS_TOPIC
@Controller
@SuppressWarnings("unchecked")
@RequestMapping("/manage/complex")
public class BbsTopicController
{
	private BbsTopicService service;
	private BbsReplyService bbsReplyService;

	//业务对象注入
	public void setBbsTopicService(BbsTopicService service)
	{
		this.service = service;
	}
	
	public void setBbsReplyService(BbsReplyService bbsReplyService)
	{
		this.bbsReplyService = bbsReplyService;
	}

	//添加
	@RequestMapping("/addBbsTopic{viewName}")
	public ModelAndView addBbsTopic(HttpServletResponse response,HttpServletRequest request, BbsTopic po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			result.setViewName("/manage/complex/addBbsTopic.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				String user_name = UserShare.getCurrentUserName(request);//用户名称
				String user_code = UserShare.getCurrentUserCode(request);//用户代码
				po.setUser_code(user_code);
				po.setUser_name(user_name);
				po.setCreate_date(new Date());
				service.add(po);
				writer.write("发布成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("发布失败！" + e.getCause());
			}
		}
		return result;
	}

	//删除
	@RequestMapping("/delBbsTopic")
	public void delBbsTopic(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		MyRequest req = new MyRequest(request);
		service.delByIds(CollectionUtil.toLongArray(req.getLongArray("keyIndex", 0)));
		response.sendRedirect(req.getRefererURL());
	}

	//修改
	@RequestMapping("/updBbsTopic{viewName}")
	public ModelAndView updBbsTopic(HttpServletRequest request, HttpServletResponse response, BbsTopic po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		MyRequest req = new MyRequest(request);
		if ("1".equals(viewName))
		{
			Long id = req.getLong("keyIndex");
			po = service.getById(id);
			result = new ModelAndView();
			Map map = new HashMap();
			map.put("topic_id", id);
			List<BbsReply> replyList = bbsReplyService.getList(map);
			result.addObject("replyList",replyList);
			result.addObject("po", po);
			result.setViewName("/manage/complex/updBbsTopic.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				String content = req.getString("content");//回复内容
				String user_name = UserShare.getCurrentUserName(request);//回复用户名称
				String user_code = UserShare.getCurrentUserCode(request);//回复用户代码
				Integer topic_id = po.getId();//回复的主题ID
				BbsReply reply = new BbsReply();
				reply.setContent(content);
				reply.setTopic_id(topic_id);
				reply.setCreate_date(new Date());
				reply.setUser_code(user_code);
				reply.setUser_name(user_name);
				bbsReplyService.add(reply);
				//writer.write("发表成功！");
				//result = new ModelAndView("redirect:updBbsTopic1.htm");
				//response.sendRedirect("updBbsTopic1.htm?keyIndex="+po.getId());
				request.getRequestDispatcher("updBbsTopic1.htm?keyIndex="+po.getId()).forward(request, response);
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("发表失败！" + e.getCause());
			}
		}
		return result;
	}

	//获得分页
	@RequestMapping("/getBbsTopic")
	public ModelAndView getBbsTopic(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="page", defaultValue="1")int page)
	{
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		Map map = req.getParameterValueMap(false, true);
		PageModel<BbsTopic> pageModel = service.getPage(page, pageSize, map);
		ModelAndView result = new ModelAndView();
		PageNav pageNav = new PageNav(request, pageModel);
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		result.setViewName("/manage/complex/getBbsTopic.jsp");
		return result;
	}

	//明细
	@RequestMapping("/getBbsTopicById")
	public ModelAndView getBbsTopicById(HttpServletRequest request, HttpServletResponse response)
	{
		BbsTopic po = null;
		MyRequest req = new MyRequest(request);
		Long id = req.getLong("keyIndex");
		po = service.getById(id);
		ModelAndView result = new ModelAndView();
		result.addObject("po", po);
		result.setViewName("/manage/complex/getBbsTopicById.jsp");
		return result;
	}
	
}
